//
//  RealNameViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 实名认证

#import "RealNameViewController.h"
#import "SystemSetingCell.h"
#import "RealNameHeaderView.h"
#import "DetailRealNameViewController.h"
#import "UploadIdCardViewController.h"

@interface RealNameViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;

/**
 dataArr
 */
@property (nonatomic , strong) NSArray *dataArr;
/**
 requestDataArr
 */
@property (nonatomic , strong) NSMutableArray *requestDataArr;
/**
 headerView
 */
@property (nonatomic , strong) RealNameHeaderView *headerView;


@property (nonatomic, strong) RealName *nameModel;



@end

@implementation RealNameViewController


#pragma mark - view life cycle

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initData];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.baseNavView.navTitleLabel.text = @"实名认证";
    [self setUpView];
}


#pragma mark - init method

- (void)setUpView
{
    [self initView];
    [self initLayout];
    [self initAction];
}


- (void)initView
{
    [self.view addSubview:self.myTableView];
}

- (void) initLayout
{
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}

- (void) initAction
{
    
}

- (void)initData {
    
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    
    self.dataArr = @[@{@"title":@"个人信息",@"detail":@""},
                     @{@"title":@"证件上传",@"detail":@"未上传"}];
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] getRealNameMessageWithMemberId:memberId] subscribeNext:^(RealName * _Nullable realName) {
        @strongify(self);
        
        self.nameModel = realName;
        
        NSString *value = [realName.status intValue] == 0 ?@"未上传" :@"已上传";
        self.dataArr = @[@{@"title":@"个人信息",@"detail":@""},
                         @{@"title":@"证件上传",@"detail":value}];
        
        [self.myTableView reloadData];

    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 260;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    self.headerView = [[RealNameHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
    self.headerView.backgroundColor = [UIColor globalBackgroundColor];
    self.headerView.nameModel = self.nameModel;
    return self.headerView;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemSetingCell *cell = [SystemSetingCell cellWithTableView:tableView];
    cell.infoDic = self.dataArr[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DetailRealNameViewController *detailVC = [[DetailRealNameViewController alloc] init];
        detailVC.nameModel = self.nameModel;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else {
        UploadIdCardViewController *detailVC = [[UploadIdCardViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}


#pragma mark - target action

#pragma mark - private method

#pragma mark - getter and setter

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 54;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorColor = mIconLineColor;
    }
    return _myTableView;
}


- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}


- (NSMutableArray *)requestDataArr {
    if (_requestDataArr) {
        _requestDataArr = [[NSMutableArray alloc] init];
    }
    return _requestDataArr;
}

@end
